<template>
  <el-container class="home-contanier">
    <!-- 头部区域 -->
    <el-header>
      <div :class="isCollapse ? 'toggle-button-left' : 'toggle-button'">
        <i
          :class="isCollapse ? 'el-icon-s-unfold' : 'el-icon-s-fold'"
          @click="toggleCollapse"
          style="margin: 0 10px"
        ></i>
        <h5>综合服务管理平台</h5>
      </div>
      <el-button type="info" @click="logout">退出</el-button>
    </el-header>
    <!-- 页面主体区域 -->
    <el-container>
      <!-- 侧边栏 -->
      <el-aside :width="isCollapse ? '64px' : '200px'" class="home-aside">
        <div>
          <!-- <img src="../assets/cat.png" alt="宠泰泰" class="home-logo" /> -->
          <el-avatar :src="logoUrl"></el-avatar>
          <span v-show="!isCollapse">宠泰泰</span>
        </div>
        <!-- 侧边栏 菜单区域-->
        <el-menu
          background-color="#333744"
          text-color="#fff"
          active-text-color="#4cb382"
          unique-opened
          :collapse="isCollapse"
          :collapse-transition="false"
          router
          :default-active="activePath"
        >
          <!-- 一级菜单 -->
          <el-submenu
            :index="item.id + ''"
            v-for="item in menulist"
            :key="item.id"
          >
            <!-- 一级菜单的模板区域 -->
            <template slot="title">
              <!-- 图标 -->
              <!-- <i :class="item.icon"></i> -->
              <!-- <el-image
                :src="require('@/assets/images/icons/pet.png')"
                fit="cover"
                style="margin-right: 5px"
              ></el-image> -->
              <el-image
                :src="require('@/' + item.icon)"
                fit="cover"
                style="margin-right: 5px"
              ></el-image>
              <!-- 文本 -->
              <span>{{ item.authName }}</span>
            </template>
            <!-- 二级菜单 -->
            <el-menu-item
              :index="'/' + subItem.path"
              v-for="subItem in item.children"
              :key="subItem.id"
              @click="saveNarState('/' + subItem.path)"
            >
              <!-- 图标 -->
              <i class="el-icon-menu"></i>
              <!-- 文本 -->
              <span>{{ subItem.authName }}</span>
            </el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>
      <!-- 右侧内容主体 -->
      <el-main>
        <!-- 路由占位符 -->
        <router-view @updateActivePath="updateActivePath"></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
export default {
  data() {
    return {
      // 左侧菜单数据
      menulist: [],
      inconsObj: {},
      // 是否折叠
      isCollapse: false,
      // 被激活的链接地址
      activePath: '',
      logoUrl: require('../assets/logo.png')
    }
  },
  created() {
    this.getMenuList()
    this.activePath = window.sessionStorage.getItem('activePath')
  },
  methods: {
    logout() {
      window.sessionStorage.clear()
      this.$router.push('/login')
    },
    // 获取所有的菜单
    async getMenuList() {
      const { data: res } = await this.$http.post('admin/user/menu')
      // console.log(res)
      if (res.meta.status !== 200) return this.$message.error(res.meta.message)
      this.menulist = res.data
    },
    // 点击按钮，切换菜单的折叠与展开
    toggleCollapse() {
      this.isCollapse = !this.isCollapse
    },
    // 保存链接的激活状态
    saveNarState(activePath) {
      window.sessionStorage.setItem('activePath', activePath)
    },
    updateActivePath(path) {
      this.activePath = path
    }
  },
  watch: {}
}
</script>

<style lang="less" scoped>
.home-contanier {
  height: 100%;
}
.home-logo {
  width: 45px;
}
.el-header {
  background-color: #373d41;
  display: flex;
  justify-content: space-between;
  padding-left: 0;
  align-items: center;
  color: #fff;
  font-size: 20px;
  > div {
    display: flex;
    align-items: center;
  }
}
.el-aside {
  background-color: #333744;
  margin-top: -60px;
  position: relative;
  color: #fff;
  .el-menu {
    border-right: none;
  }
  > div {
    display: flex;
    align-items: center;
    height: 60px;
    span {
      margin-left: 15px;
      font-size: 20px;
    }
  }
}
.el-main {
  background-color: #eaedf1;
}
.toggle-button {
  position: relative;
  left: 206px;
  text-align: center;
  cursor: pointer;
}
.toggle-button-left {
  position: relative;
  left: 70px;
  text-align: center;
  cursor: pointer;
}
</style>
